package com.eagle.view.activity.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.eagle.R;
import com.eagle.view.animation.TransitionHelper;
import com.eagle.view.widget.LoadingDialog;

/**
 * Created by gc on 2016/12/6. Base activity
 */
public class BaseActivity extends AppCompatActivity {

    private LoadingDialog mDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setDarkStatusIcon(true);
    }

    /**
     * 设置沉浸状态栏图表颜色
     *
     * @param dark 是否暗色
     */
    public void setDarkStatusIcon(boolean dark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (dark) {
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }

    /**
     * 位移动画
     */
    protected void initExplodeTransition() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Explode enterTransition = new Explode();
            enterTransition.setInterpolator(new AccelerateDecelerateInterpolator());
            enterTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
            getWindow().setEnterTransition(enterTransition);

            Fade exitTransition = new Fade();
            exitTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
            getWindow().setExitTransition(exitTransition);
            getWindow().setReturnTransition(exitTransition);
        }
    }

    @SuppressWarnings("unchecked")
    public void transitionTo(Intent i) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            final Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(this, true);
            ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pairs);
            startActivity(i, transitionActivityOptions.toBundle());
        } else {
            startActivity(i);
        }
    }

    /**
     * 渐隐动画
     */
    protected void initFadeTransition() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Fade enterTransition = new Fade();
            enterTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
            getWindow().setEnterTransition(enterTransition);

            Fade exitTransition = new Fade();
            exitTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
            getWindow().setExitTransition(exitTransition);
            getWindow().setReturnTransition(exitTransition);
        }
    }

    public void showLoading() {
        try {
            mDialog = new LoadingDialog(this);
            mDialog.show();
        } catch (Exception e) {
            mDialog = null;
        }
    }

    public void dismissLoading() {
        try {
            if (null != mDialog && mDialog.isShowing() && !isFinishing()) {
                mDialog.cancel();
                mDialog = null;
            }
        } catch (Exception e) {

        }
    }

    public boolean isLoading() {
        if (mDialog != null) {
            return mDialog.isShowing();
        }
        return false;
    }
}
